package com.quadricopter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class Quadricopter extends Activity implements OnTouchListener
{ 
	int currentSpeed = 1;
	boolean inFlight = false;
	
	// this is the shortest duration for a toast: 2 seconds
	int duration = Toast.LENGTH_SHORT;

	// colours and filter for buttons when pressed
	int ALPHA_DOWN = 155;
	int RED_DOWN = 255;
	int GREEN_DOWN = 0;
	int BLUE_DOWN = 0;

	// colours and filter for speed indicators
	int ALPHA_SPEED = 255;
	int RED_SPEED = 255;
	int GREEN_SPEED = 165;
	int BLUE_SPEED = 0;

	int VIBE_TIME = 50; // this is in ms
	
	private String path = "/sdcard/DCIM/100ANDRO/MOV_0070.mp4";
	private VideoView mVideoView;
	private TextView instructions;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ImageView imgButton_up = (ImageView)findViewById(R.id.imgbutton_up);
		ImageView imgButton_down = (ImageView)findViewById(R.id.imgbutton_down);
		ImageView imgButton_forward = (ImageView)findViewById(R.id.imgbutton_forward);
		ImageView imgButton_backward = (ImageView)findViewById(R.id.imgbutton_backward);
		ImageView imgButton_left = (ImageView)findViewById(R.id.imgbutton_left);
		ImageView imgButton_right = (ImageView)findViewById(R.id.imgbutton_right);
		ImageView imgButton_left_turn = (ImageView)findViewById(R.id.imgbutton_left_turn);
		ImageView imgButton_right_turn = (ImageView)findViewById(R.id.imgbutton_right_turn);
		ImageView imgButton_faster = (ImageView)findViewById(R.id.imgbutton_faster);
		ImageView imgButton_slower = (ImageView)findViewById(R.id.imgbutton_slower);
		
		
		imgButton_up.setOnTouchListener(this);
		imgButton_down.setOnTouchListener(this);
		imgButton_forward.setOnTouchListener(this);
		imgButton_backward.setOnTouchListener(this);
		imgButton_left.setOnTouchListener(this);
		imgButton_right.setOnTouchListener(this);
		imgButton_left_turn.setOnTouchListener(this);
		imgButton_right_turn.setOnTouchListener(this);

		imgButton_faster.setOnTouchListener(this);
		imgButton_slower.setOnTouchListener(this);

	}
	@Override
	public boolean onTouch(View v, MotionEvent me) {
		ImageView imgButton_up = (ImageView)findViewById(R.id.imgbutton_up);
		ImageView imgButton_down = (ImageView)findViewById(R.id.imgbutton_down);
		ImageView imgButton_forward = (ImageView)findViewById(R.id.imgbutton_forward);
		ImageView imgButton_backward = (ImageView)findViewById(R.id.imgbutton_backward);
		ImageView imgButton_left = (ImageView)findViewById(R.id.imgbutton_left);
		ImageView imgButton_right = (ImageView)findViewById(R.id.imgbutton_right);
		ImageView imgButton_left_turn = (ImageView)findViewById(R.id.imgbutton_left_turn);
		ImageView imgButton_right_turn = (ImageView)findViewById(R.id.imgbutton_right_turn);
		ImageView imgButton_faster = (ImageView)findViewById(R.id.imgbutton_faster);
		ImageView imgButton_slower = (ImageView)findViewById(R.id.imgbutton_slower);
		ImageView img_speed2 = (ImageView)findViewById(R.id.img_speed2);
		ImageView img_speed3 = (ImageView)findViewById(R.id.img_speed3);
		
		EditText msgBox = (EditText)findViewById(R.id.msgBox);

		Context context = getApplicationContext();
		Vibrator vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE) ;

		if (inFlight)
		{
			if (me.getAction() == MotionEvent.ACTION_DOWN) {
				CharSequence text = "";
				switch (v.getId()){
				case R.id.imgbutton_up:
					//imgButton_up.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					//imgButton_up.setBackgroundColor(android.graphics.Color.WHITE);
					text = "Up, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_down:
					//imgButton_down.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Down, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_forward:
					//imgButton_forward.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Forward, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_backward:
					//imgButton_backward.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Backward, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_left:
					//imgButton_left.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Left, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_right:
					//imgButton_right.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Right, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_left_turn:
					//imgButton_left_turn.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Left Turn, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_right_turn:
					//imgButton_right_turn.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					text = "Right Turn, Speed = " + currentSpeed;
					vibe.vibrate(VIBE_TIME);
					break;
				case R.id.imgbutton_faster:
					vibe.vibrate(VIBE_TIME);
					//imgButton_faster.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					if (currentSpeed == 1)
					{
						img_speed2.setColorFilter(Color.argb(ALPHA_SPEED, RED_SPEED, GREEN_SPEED, BLUE_SPEED));
						currentSpeed++;
						text = "Speed increased to " + currentSpeed;
					}
					else if (currentSpeed == 2)
					{
						img_speed3.setColorFilter(Color.argb(ALPHA_SPEED, RED_SPEED, GREEN_SPEED, BLUE_SPEED));
						currentSpeed++;
						text = "Speed increased to " + currentSpeed;
					}
					break;
				case R.id.imgbutton_slower:
					vibe.vibrate(VIBE_TIME);
					//imgButton_slower.setColorFilter(Color.argb(ALPHA_DOWN, RED_DOWN, GREEN_DOWN, BLUE_DOWN));
					if (currentSpeed == 3)
					{
						img_speed3.clearColorFilter();
						currentSpeed--;
						text = "Speed decreased to " + currentSpeed;
					}
					else if (currentSpeed == 2)
					{
						img_speed2.clearColorFilter();
						currentSpeed--;
						text = "Speed decreased to " + currentSpeed;
					}
				}
				//Toast.makeText(context, text, duration).show();
				
				msgBox.setText(text);

				return true;
			} 
			/*else if (me.getAction() == MotionEvent.ACTION_UP) {
				switch (v.getId()){
				case R.id.imgbutton_up:
					//imgButton_up.setBackgroundColor(android.graphics.Color.BLACK);
					imgButton_up.clearColorFilter();
					break;
				case R.id.imgbutton_down:
					imgButton_down.clearColorFilter();
					break;
				case R.id.imgbutton_forward:
					imgButton_forward.clearColorFilter();
					break;
				case R.id.imgbutton_backward:
					imgButton_backward.clearColorFilter();
					break;
				case R.id.imgbutton_left:
					imgButton_left.clearColorFilter();
					break;
				case R.id.imgbutton_right:
					imgButton_right.clearColorFilter();
					break;
				case R.id.imgbutton_left_turn:
					imgButton_left_turn.clearColorFilter();
					break;
				case R.id.imgbutton_right_turn:
					imgButton_right_turn.clearColorFilter();
					break;
				case R.id.imgbutton_faster:
					imgButton_faster.clearColorFilter();
					break;
				case R.id.imgbutton_slower:
					imgButton_slower.clearColorFilter();
					break;
				}
				return true;
			}*/
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {    
		MenuInflater inflater = getMenuInflater();    
		inflater.inflate(R.menu.takeoff_land_menu, menu);    
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {   
		Context context = getApplicationContext();
		CharSequence text = "";
		ImageView img_speed1 = (ImageView)findViewById(R.id.img_speed1);
		ImageView img_speed2 = (ImageView)findViewById(R.id.img_speed2);
		ImageView img_speed3 = (ImageView)findViewById(R.id.img_speed3);
		
		EditText msgBox = (EditText)findViewById(R.id.msgBox);
		
		// Handle item selection    
		switch (item.getItemId()) {    
		case R.id.takeoff:        
			if (!inFlight)
			{
				text = "Takeoff Sequence Selected";  
				inFlight = true;
				img_speed1.setColorFilter(Color.argb(ALPHA_SPEED, RED_SPEED, GREEN_SPEED, BLUE_SPEED));
				currentSpeed = 1;
				mVideoView = (VideoView) findViewById(R.id.VideoView);
				//use this to get touch events  
				mVideoView.requestFocus();
				mVideoView.setVideoPath(path); 
				//enable this if you want to enable video controllers, such as pause and forward
				//mVideoView.setMediaController(new MediaController(this)); 
				//plays the movie
				mVideoView.start();
				instructions = (TextView) findViewById(R.id.instructions);
				instructions.setVisibility(View.GONE);
			}
			break;   
		case R.id.land:
			if (inFlight)
			{
				text = "Landing Sequence Selected"; 
				inFlight = false;
				img_speed1.clearColorFilter();
				img_speed2.clearColorFilter();
				img_speed3.clearColorFilter();
				mVideoView.stopPlayback();
				instructions.setVisibility(View.VISIBLE);
			}
			break;  
		}
		//Toast.makeText(context, text, duration).show();
		msgBox.setText(text);
		return true;
	}
}